package com.daos;

import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Service;

import com.model.Activity;
import com.model.Task;

@Service
public class ActivityDao extends AbstractDao<Activity> {

	public Activity getActivityById(long id) {
		Activity activity =  (Activity) hibernateTemplate.find("from Activities a where a.id = ? and a.visible = true" ,id).get(0);
		return activity != null ?  activity: null;
	}

	public List<Activity> getActivitiesForEmployee(int id) {
		@SuppressWarnings("unchecked")
		List<Activity> activities =   hibernateTemplate.find("from Activities a where a.employeeCreator.id = ? and a.progress !=100 and a.visible = true",id);
		return activities != null ?  activities: null;
	}

	public List<Activity> getAllActivities() {
		@SuppressWarnings("unchecked")
		List<Activity> activities =   hibernateTemplate.find("from Activities a where a.visible = true");
		return activities != null ?  activities: null;
	}

	public List<Activity> getRelatedActivitiesForEmployee(int id) {
		String query = "select distinct t.activity from Tasks t where t.employee.id  = "+id+" and t.activity.visible = true and t.activity.progress != 100 and t.visible = true order by t.finishDate asc";
		@SuppressWarnings("unchecked")
		List<Activity> activities =   hibernateTemplate.find(query);
		return activities;
	}

	public List<Activity> getActivitiesForEmployeeOrderByTaskDeadLine() {
		@SuppressWarnings("unchecked")
		List<Activity> activities =   hibernateTemplate.find("select distinct t.activity from Tasks t where t.visible = true and t.activity.visible = true order by t.finishDate asc");
		List<Activity> activitiesWithoutTasks = hibernateTemplate.find("from Activities a where a.visible = true and  a.id not in (select t.activity from Tasks t where t.visible = true and t.activity.visible = true)");
		activities.addAll(activitiesWithoutTasks);
		return activities != null ?  activities: null;
	}

	public List<Activity> getAllActivitiesWithTasksToBeApproved() {
		List<Activity> activities =   hibernateTemplate.find("select distinct t.activity from Tasks t where t.visible = true and t.activity.visible = true and t.progress = 75 order by t.finishDate asc");
		return activities != null ?  activities: null;
	}

	
}
